package org.rfs.ipodencoder;

import com.trolltech.qt.core.Qt;
import com.trolltech.qt.core.Qt.WindowFlags;
import com.trolltech.qt.gui.*;

public class IPodEncoderFFMpegEncodingProgressView extends QDialog {

    IPodEncoderFFMpegEncodingProgress ui = new IPodEncoderFFMpegEncodingProgress();
    
    public IPodEncoderFFMpegEncodingProgressView() {
        ui.setupUi(this);
    }

    public IPodEncoderFFMpegEncodingProgressView(QWidget parent) {
        super(parent, new WindowFlags(Qt.WindowType.WindowTitleHint));
        ui.setupUi(this);
    }
    
    public void on_pushButton_cancel_encode_clicked() {
    	QMessageBox.information(this, "Not yet!", "Not yet implemented...hang on", QMessageBox.StandardButton.Ok);
    }
    
    public void updateStatus(FFMpegEncodeStatus encodeStatus) {
    	long lCurrentTime = System.currentTimeMillis();
    	long lPassedTime = lCurrentTime - encodeStatus.starttime;
    	
    	long lTimeLeft = (((lPassedTime / Long.parseLong(encodeStatus.frame)) * encodeStatus.totalframes) - lPassedTime) / 1000;
    	ui.progressBar.setMaximum((int)encodeStatus.totalframes);
    	ui.progressBar.setValue(Integer.parseInt(encodeStatus.frame));
    	
    	setWindowTitle("Encoding " + encodeStatus.pass + ": " + encodeStatus.filename.substring(encodeStatus.filename.lastIndexOf("/")+1, encodeStatus.filename.length()));
    	
    	ui.lineEdit_frame.setText(encodeStatus.frame);
    	ui.lineEdit_fps.setText(encodeStatus.fps);
    	ui.lineEdit_quantizer.setText(encodeStatus.q);
    	ui.lineEdit_size.setText(encodeStatus.size);
    	ui.lineEdit_time.setText(encodeStatus.time);
    	ui.lineEdit_bitrate.setText(encodeStatus.bitrate);
    	ui.lineEdit_timeleft.setText(getTimeLeftString(lTimeLeft));
    }
    
	private String getTimeLeftString(long timeLeft) {
		if (timeLeft > 75) 
			return String.valueOf(Math.round(timeLeft / 60) + " min");
		else if (timeLeft > 45)
			return "about a minute";
		else
			return "less than a minute";
	}
}
